package de.justphil.tcg.tcgserver.commons.http;

import de.justphil.tcg.tcgserver.commons.i18n.Error;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class MessageResponseCode {
	public static final int OK						= 200;
	
	public static final int BAD_REQUEST				= 400;
	public static final int FORBIDDEN				= 403;
	public static final int NOT_FOUND				= 404;
	
	public static final int INTERNAL_SERVER_ERROR	= 500;
	public static final int SERVICE_UNAVAILABLE		= 503;
	
	public static int getResponseCodeByErrorKey(String errorKey) {
		Integer code = codes.get(errorKey);
		
		if (code == null) {
			return 0;
		}
		else {
			return code.intValue();
		}
	}
	
	private static final ConcurrentMap<String, Integer> codes = new ConcurrentHashMap<String, Integer>();
	static {
		codes.put(Error.CLIENT_ERROR_MALFORMED_WEBSOCKET_MESSAGE,		BAD_REQUEST);
		codes.put(Error.CLIENT_ERROR_UNKNOWN_WEBSOCKET_MESSAGE_SUBJECT,	BAD_REQUEST);
		codes.put(Error.CLIENT_ERROR_INVALID_WEBSOCKET_MESSAGE_SUBJECT,	BAD_REQUEST);
		codes.put(Error.CLIENT_ERROR_GAME_ID_PLAYER_ID_OR_OTP_MISSING,	BAD_REQUEST);
		codes.put(Error.CLIENT_ERROR_GAME_NOT_AVAILABLE,				NOT_FOUND);
		codes.put(Error.CLIENT_ERROR_GAME_EXPIRED,						NOT_FOUND);
		codes.put(Error.CLIENT_ERROR_PLAYER_NOT_ALLOWED,				FORBIDDEN);
		codes.put(Error.CLIENT_ERROR_PLAYER_ALREADY_CONNECTED,			FORBIDDEN);
		codes.put(Error.CLIENT_ERROR_OTP_WRONG,							FORBIDDEN);
		codes.put(Error.SERVER_ERROR_INTERNAL,							INTERNAL_SERVER_ERROR);
	}
}
